/* global document */
import React from 'react'
import PropTypes from 'prop-types'
import {Button, Col, DatePicker, Form, Input, message, Row, Select} from 'antd'

const {Option} = Select
const FormItem = Form.Item

const ColProps = {
  xs: 6,
  sm: 6,
  style: {
    marginBottom: 6,
  },
}

const formItemLayout = {
  labelCol: {span: 6},
  wrapperCol: {span: 18},
  style: {
    marginBottom: 10,
  },
}

const TwoColProps = {
  ...ColProps,
  xl: 96,
}

const Filter = ({
                  departments,
                  onFilterChange,
                  form: {
                    getFieldDecorator,
                    getFieldsValue,
                    setFieldsValue,
                  },
                }) => {

  const valid = fields => {
    const {type, keyword} = fields

    if (!type && keyword) {
      message.warn("请选择查询类型")
      return false
    }

    return true
  }

  const handleSubmit = () => {
    let fields = getFieldsValue()
    if (!valid(fields)) {
      return
    }
    const {beginOperationTime, endOperationTime} = fields
    if (beginOperationTime) {
      fields.beginOperationTime = beginOperationTime.format('YYYY-MM-DD hh:mm:ss')
    }
    if (endOperationTime) {
      fields.endOperationTime = endOperationTime.format('YYYY-MM-DD hh:mm:ss')
    }
    onFilterChange(fields)
  }

  const handleReset = () => {
    const fields = getFieldsValue()
    for (let item in fields) {
      if ({}.hasOwnProperty.call(fields, item)) {
        if (fields[item] instanceof Array) {
          fields[item] = []
        } else {
          fields[item] = undefined
        }
      }
    }
    setFieldsValue(fields)
    handleSubmit()
  }

  return (
    <div>
      <Row gutter={24}>
        <Col md={6} sm={24}>
          <FormItem {...formItemLayout} label="运营微号">
            {getFieldDecorator('operatorWxId', {})(<Input placeholder="请输入"/>)}
          </FormItem>
        </Col>
        <Col md={6} sm={24}>
          <FormItem {...formItemLayout} label="归属员工">
            {getFieldDecorator('belongEmpId', {})(<Input placeholder="请输入"/>)}
          </FormItem>
        </Col>
        <Col md={6} sm={24}>
          <FormItem {...formItemLayout} label="敏感类型">
            {getFieldDecorator('sensitiveType',)(
              <Select placeholder={"请选择"} style={{width: '100%'}} alowClear>
                <Option value={0}>删除好友</Option>
                <Option value={1}>拉黑好友</Option>
                <Option value={2}>推荐名片给好友</Option>
                <Option value={3}>删除消息</Option>
                <Option value={4}>清除消息</Option>
                <Option value={5}>聊天包含敏感词</Option>
                <Option value={6}>向外转账</Option>
                <Option value={7}>向外发送红包</Option>
                <Option value={8}>发送语音消息</Option>
                <Option value={9}>提现</Option>
                <Option value={10}>撤回消息</Option>
                <Option value={11}>退登微信</Option>
                <Option value={12}>发送号码</Option>
                <Option value={13}>发送文件</Option>
                <Option value={14}>发送视频</Option>
                <Option value={15}>其他风控</Option>
              </Select>,
            )}
          </FormItem>
        </Col>
        <Col md={6} sm={24}>
          <FormItem {...formItemLayout} label="处理状态">
            {getFieldDecorator('processStatus',)(
              <Select placeholder={"请选择"} style={{width: '100%'}} alowClear>
                <Option value={0}>未处理</Option>
                <Option value={1}>已处理</Option>
              </Select>,
            )}
          </FormItem>
        </Col>

      </Row>
      <Row gutter={24}>
        <Col md={6} sm={24}>
          <FormItem {...formItemLayout} label="是否有订单" alowClear>
            {getFieldDecorator('hasOrder',)(
              <Select placeholder={"请选择"} style={{width: '100%'}}>
                <Option value={0}>没有</Option>
                <Option value={1}>有</Option>
              </Select>,
            )}
          </FormItem>
        </Col>
        <Col md={6} sm={24}>
          <FormItem {...formItemLayout} label="公司部门">
            {getFieldDecorator('deptId',)(
              <Select placeholder={"请选择"} style={{width: '100%'}} alowClear>
                {departments.map(_ => <Option value={_.id}>{_.name}</Option>)}
              </Select>,
            )}
          </FormItem>
        </Col>
        <Col {...ColProps} xl={{span: 8}} md={4} sm={24}>
          <FormItem labelCol={{span: 4}} wrapperCol={{span: 18}} label="操作时间">
            <Col span={11}>
              {getFieldDecorator('beginOperationTime')(<DatePicker style={{width: '100%'}} placeholder="开始时间"/>)}
            </Col>
            <Col span={2}>
              <span style={{display: 'inline-block', width: '100%', textAlign: 'center'}}>
                -
              </span>
            </Col>
            <Col span={11}>
              {getFieldDecorator('endOperationTime')(<DatePicker style={{width: '100%'}} placeholder="结束时间"/>)}
            </Col>
          </FormItem>
        </Col>
        <Col {...TwoColProps} xl={{span: 4}} md={{span: 24}} sm={{span: 24}}>
          <div style={{display: 'flex', justifyContent: 'space-between', flexWrap: 'wrap'}}>
            <div>
              <Button type="primary" className="margin-right" onClick={handleSubmit}>查询</Button>
              <Button onClick={handleReset}>重置</Button>
            </div>
          </div>
        </Col>
      </Row>
    </div>
  )
}

Filter.propTypes = {
  onAdd: PropTypes.func,
  form: PropTypes.object,
  filter: PropTypes.object,
  onFilterChange: PropTypes.func,
}

export default Form.create()(Filter)
